package com.zz.nondatain.view.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.TextView;

import androidx.viewbinding.ViewBinding;

import com.zz.nondatain.R;
import com.zz.nondatain.base.BaseActivity;
import com.zz.nondatain.databinding.ActivityAttendanceStatsBinding;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;
import java.util.Random;

/**
 * 考勤统计页面，展示考勤数据的汇总统计
 */
public class AttendanceStatsActivity extends BaseActivity<ActivityAttendanceStatsBinding> {

    private boolean isMonthView = true; // 默认显示月视图
    private Calendar currentDate; // 当前选择的日期

    @Override
    protected void initView() {
        // 设置标题
        binding.tvTitle.setText("统计");
        
        // 设置用户信息
        binding.tvUserName.setText("张膨山1");
        binding.tvUserId.setText("编号：13010220240100001");
        
        // 初始化日期
        currentDate = Calendar.getInstance();
        
        // 初始视图类型
        updateViewType();
        
        // 更新日期显示
        updateDateDisplay();
        
        // 更新统计数据
        updateStatistics();
    }

    @Override
    protected void initData() {
        
    }

    @Override
    protected void initObservers() {
        setupListeners();
    }

    @Override
    protected ActivityAttendanceStatsBinding getViewBinding() {
        return ActivityAttendanceStatsBinding.inflate(getLayoutInflater());
    }
    
    private void setupListeners() {
        // 返回按钮点击事件
        binding.btnBack.setOnClickListener(v -> finish());
        
        // 月视图按钮点击事件
        binding.btnMonthView.setOnClickListener(v -> {
            isMonthView = true;
            updateViewType();
            updateDateDisplay();
            updateStatistics();
        });
        
        // 周视图按钮点击事件
        binding.btnWeekView.setOnClickListener(v -> {
            isMonthView = false;
            updateViewType();
            updateDateDisplay();
            updateStatistics();
        });
        
        // 上一个时间段按钮
        binding.btnPrevDate.setOnClickListener(v -> {
            if (isMonthView) {
                currentDate.add(Calendar.MONTH, -1);
            } else {
                currentDate.add(Calendar.WEEK_OF_YEAR, -1);
            }
            updateDateDisplay();
            updateStatistics();
        });
        
        // 下一个时间段按钮
        binding.btnNextDate.setOnClickListener(v -> {
            if (isMonthView) {
                currentDate.add(Calendar.MONTH, 1);
            } else {
                currentDate.add(Calendar.WEEK_OF_YEAR, 1);
            }
            updateDateDisplay();
            updateStatistics();
        });
    }
    
    /**
     * 更新日期显示
     */
    private void updateDateDisplay() {
        SimpleDateFormat dateFormat;
        if (isMonthView) {
            dateFormat = new SimpleDateFormat("yyyy-MM", Locale.getDefault());
            binding.tvDateDisplay.setText(dateFormat.format(currentDate.getTime()));
        } else {
            // 获取当前周的开始和结束日期
            Calendar weekStart = (Calendar) currentDate.clone();
            weekStart.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
            Calendar weekEnd = (Calendar) weekStart.clone();
            weekEnd.add(Calendar.DAY_OF_WEEK, 6); // 周日
            
            SimpleDateFormat weekFormat = new SimpleDateFormat("MM.dd", Locale.getDefault());
            String startDate = weekFormat.format(weekStart.getTime());
            String endDate = weekFormat.format(weekEnd.getTime());
            binding.tvDateDisplay.setText(startDate + "-" + endDate);
        }
    }
    
    /**
     * 更新视图类型（月/周）
     */
    private void updateViewType() {
        if (isMonthView) {
            binding.btnMonthView.setSelected(true);
            binding.btnWeekView.setSelected(false);
        } else {
            binding.btnMonthView.setSelected(false);
            binding.btnWeekView.setSelected(true);
        }
    }
    
    /**
     * 更新统计数据
     */
    private void updateStatistics() {
        // 这里使用随机数模拟数据，实际应用中应从API获取
        Random random = new Random();
        int normalDays = isMonthView ? random.nextInt(20) + 10 : random.nextInt(5) + 2;
        int missedDays = isMonthView ? random.nextInt(5) : random.nextInt(2);
        int absentDays = isMonthView ? random.nextInt(3) : random.nextInt(1);
        int outOfBoundsDays = isMonthView ? random.nextInt(2) : 0;
        
        // 设置统计数据
        binding.tvNormalDays.setText(String.valueOf(normalDays));
        binding.tvMissedDays.setText(String.valueOf(missedDays));
        binding.tvAbsentDays.setText(String.valueOf(absentDays));
        binding.tvOutOfBoundsDays.setText(String.valueOf(outOfBoundsDays));
        
        // 设置统计标题
        SimpleDateFormat titleFormat = new SimpleDateFormat("yyyy年MM月", Locale.getDefault());
        String title = titleFormat.format(currentDate.getTime()) + (isMonthView ? "月统计" : "周统计");
        binding.tvStatsTitle.setText(title);
    }
} 